﻿package cn.antscript.airCMS.display.component.btnbase 
{
	import cn.antscript.airCMS.data.singleData.AbstractData;
	import cn.antscript.airCMS.data.singleData.ImgData;
	import cn.antscript.airCMS.display.component.EditPanel;
	import cn.antscript.airCMS.display.component.SurePanel;
	import cn.antscript.airCMS.display.component.WaitBar;
	import cn.antscript.airCMS.display.ImgViewer;
	import cn.antscript.model.SqlModel;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.MouseEvent;
	import flash.events.ProgressEvent;
	import flash.filters.DropShadowFilter;
	import flash.net.URLRequest;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	/**
	 * ...
	 * @author 
	 */
	public class ImgBtn extends Sprite
	{
		
		public var data:ImgData;
		
		private var _editBtn:AbstractIco;
		private var _imgBtn:AbstractIco;
		private var _replaceBtn:AbstractIco;
		private var _delBtn:AbstractIco;
		private var _loader:Loader;
		private var _alert:TextField;
		
		public function ImgBtn(data_:AbstractData=null,delEnabled:Boolean=true,container:DisplayObjectContainer=null,xPos:int=0,yPos:int=0) 
		{	
			this.graphics.lineStyle(0.5, 0x333333);
			this.graphics.beginFill(0);
			this.graphics.drawRect(0, 0, 120, 120);
			this.graphics.endFill();
			
			data = data_ as ImgData;
			
			if (container != null) {
				container.addChild(this);
			}
			this.x = xPos;
			this.y = yPos;
			
			
			this.alpha = 0.1;
			this.mouseEnabled = false;
			
			_editBtn = new AbstractIco(IcoBitmapdatas.Edit, "编辑", this, 0, 130);
			_imgBtn = new AbstractIco(IcoBitmapdatas.Image, "查看", this, 30, 130);
			_replaceBtn = new AbstractIco(IcoBitmapdatas.Replace, "替换", this, 63, 130);
			
			//if (delEnabled) {
				_delBtn = new AbstractIco(IcoBitmapdatas.Delete, "删除", this, 97, 130);
			//}
			
			
			_alert = new TextField();
			_alert.width = 120;
			_alert.height = 20;
			_alert.text = "图片加载中";
			_alert.textColor = 0xffffff;
			_alert.y = 50;
			_alert.autoSize = TextFieldAutoSize.CENTER;
			addChild(_alert);
			
			_loader = new Loader();
			_loader.load(new URLRequest(data.thumb));
			_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onCom);
			_loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onPro);
			_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, onErr);
			
			this.addEventListener(MouseEvent.CLICK, onClick);		
			addEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			
			
		}
		
		private function onPro(e:ProgressEvent):void 
		{
			_alert.text = int(Number(e.bytesLoaded / e.bytesTotal) * 100).toString() + "%";
		}
		
		private function onRemove(e:Event):void 
		{
			removeEventListener(Event.REMOVED_FROM_STAGE, onRemove);
			this.removeEventListener(MouseEvent.CLICK, onClick);
			this.removeEventListener(MouseEvent.ROLL_OVER, onOver);
			this.removeEventListener(MouseEvent.ROLL_OUT, onOut);
			
			data = null;		
		}
		
		private function onCom(e:Event):void 
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCom);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErr);
			
			removeChild(_alert);
			_alert = null;
			
			_loader.x = (120 - _loader.content.width) / 2;
			_loader.y = (120 - _loader.content.height) / 2;
			addChildAt(_loader, 0);
			
			addEventListener(Event.ENTER_FRAME, onEnter);
			
			
			this.addEventListener(MouseEvent.ROLL_OVER, onOver);
			this.addEventListener(MouseEvent.ROLL_OUT, onOut);
			
			this.graphics.clear();
			this.graphics.lineStyle(0.5, 0x333333);
			this.graphics.beginFill(0,0);
			this.graphics.drawRect(-10, -10, 140, 170);
			this.graphics.endFill();
		}
		
		private function onClick(e:MouseEvent):void 
		{
			switch(e.target) {
				case _editBtn:
				EditPanel.getInstance().edit(data, this.stage);
				break;
				case _delBtn:
				SurePanel.getInstance().show("提示：一经删除则不可恢复，确定删除？",this.stage,del);
				break;
				case _imgBtn:
				ImgViewer.getInstance().show(data.url);
				break;
				case _replaceBtn:
				dispatchEvent(new Event("replace"));
				break;
			}
			
		}
		private function del():void
		{
			WaitBar.getInstance().show(this.stage);
			SqlModel.getInstance().del(data,delCom);
		}		
		private function delCom():void
		{
			WaitBar.getInstance().hide();
			dispatchEvent(new Event("deleted"));
		}
		
		
		
		
		
		private function onOver(e:MouseEvent):void 
		{
			this.graphics.clear();
			this.graphics.lineStyle(2, 0x333333);
			this.graphics.beginFill(0);
			this.graphics.drawRect(-10, -10, 140, 170);
			this.graphics.endFill();
		}
		
		private function onOut(e:MouseEvent):void 
		{
			this.graphics.clear();
			this.graphics.lineStyle(0.5, 0x333333);
			this.graphics.beginFill(0,0);
			this.graphics.drawRect(-10, -10, 140, 170);
			this.graphics.endFill();
		}
		
		private function onEnter(e:Event):void 
		{
			this.alpha += 0.05;
			if (this.alpha >= 1) {
				this.mouseEnabled = true;
				removeEventListener(Event.ENTER_FRAME, onEnter);
			}
		}
		
		private function onErr(e:IOErrorEvent):void 
		{
			_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, onCom);
			_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, onErr);
			
			_alert.text = "图片不存在";
			
		}
		
	}

}